//
//  IGCommunityDetailHeaderCell.m
//  jingdongfang
//
//  Created by BOE on 2017/8/22.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "IGCommunityDetailHeaderCell.h"

@interface IGCommunityDetailHeaderCell()

@property (nonatomic, strong) IGCommunityModel *communityModel;
@property (nonatomic, strong) UIImageView *imgView;///>背景图片
@property (nonatomic, strong) UILabel *titleLabel;///>标题
@property (nonatomic, strong) YYLabel *desLabel;///>描述
@property (nonatomic, strong) UIButton *followBtn;///>关注按钮

@end

static NSString *const kIGCommunityDetailHeaderCellID = @"kIGCommunityDetailHeaderCellID";
@implementation IGCommunityDetailHeaderCell

+ (IGCommunityDetailHeaderCell *)cellWithTableView:(UITableView *)tableView {
    IGCommunityDetailHeaderCell *cell = [tableView dequeueReusableCellWithIdentifier:kIGCommunityDetailHeaderCellID];
    if (!cell) {
        cell = [[IGCommunityDetailHeaderCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:kIGCommunityDetailHeaderCellID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.contentView.backgroundColor = [UIColor whiteColor];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self createUI];
    }
    return self;
}

- (void)createUI {
    self.contentView.backgroundColor = [UIColor c3Color];
    
    self.imgView = [UIImageView new];
    self.imgView.backgroundColor = [UIColor mainRandomColor];
    [self.contentView addSubview:self.imgView];
    
    self.titleLabel = [UILabel new];
    self.titleLabel.font = [UIFont font18];
    self.titleLabel.textColor = [UIColor c4Color];
    self.titleLabel.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.titleLabel];
    
    self.desLabel = [[YYLabel alloc] init];
    [self.contentView addSubview:self.desLabel];
    self.desLabel.backgroundColor = [UIColor clearColor];
    self.desLabel.preferredMaxLayoutWidth = KSCREENWIDTH - 2 * kGAP15;
    self.desLabel.lineBreakMode = NSLineBreakByCharWrapping;
    self.desLabel.numberOfLines = 0;
    self.desLabel.displaysAsynchronously = YES;
    self.desLabel.textColor = [UIColor c4Color];
    self.desLabel.font = [UIFont font13];
    
    self.followBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.followBtn setTitle:[NSString stringWithFormat:@"+ %@",NSLocalizedString(@"Follow", nil)] forState:UIControlStateNormal];
    [self.followBtn setTitleColor:[UIColor c3Color] forState:UIControlStateNormal];
    [self.followBtn setTitle:NSLocalizedString(@"Followed", nil) forState:UIControlStateSelected];
    [self.followBtn setTitleColor:[UIColor c5Color] forState:UIControlStateSelected];
    self.followBtn.layer.cornerRadius = 15;
    self.followBtn.backgroundColor = [UIColor appColor];
    [self.contentView addSubview:self.followBtn];
    __weak typeof(self) weakSelf = self;
    [self.followBtn setTapActionWithBlock:^{
        if (weakSelf.focusCircle) {
            weakSelf.focusCircle();
        }
    }];
    
    [self.imgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.height.mas_equalTo(210);
    }];
    
    [self.followBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.imgView.mas_bottom).offset(kGAP15);
        make.right.mas_equalTo(-kGAP15);
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(30);
    }];
    
    [self.titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(kGAP15);
        make.centerY.mas_equalTo(self.followBtn.mas_centerY);
        make.right.mas_equalTo(self.followBtn.mas_left).offset(kGAP10);
        make.height.mas_equalTo(20);
    }];
    
    [self.desLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.titleLabel);
        make.right.mas_equalTo(-kGAP15);
        make.top.mas_equalTo(self.titleLabel.mas_bottom).offset(18);
    }];
    
    self.hyb_lastViewInCell = self.desLabel;
    self.hyb_bottomOffsetToCell = 20;
}

- (void)configCellWithModel:(IGCommunityModel *)communityModel {
    _communityModel = communityModel;
    
    [_imgView sd_setImageWithURL:[NSURL URLWithString:communityModel.cImgUrl]];
    _titleLabel.text = communityModel.cTitle;
    
    NSMutableAttributedString *desText = [[NSMutableAttributedString alloc] initWithString:communityModel.cDetail];
    desText.yy_lineSpacing = 5;
    desText.yy_kern = [NSNumber numberWithFloat:.5];
    _desLabel.attributedText = desText;
    
    if ([communityModel.isFocus isEqualToString:@"1"]) {
        _followBtn.selected = YES;
        _followBtn.layer.borderWidth = 1.0;
        _followBtn.layer.borderColor = [UIColor c5Color].CGColor;
        _followBtn.backgroundColor = [UIColor c3Color];
    } else {
        _followBtn.selected = NO;
        _followBtn.layer.borderWidth = 0.f;
        _followBtn.layer.borderColor = [UIColor appColor].CGColor;
        _followBtn.backgroundColor = [UIColor appColor];
    }
}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
